package org.dong.Dao;

import org.dong.Jdbc.JdbcUtil;
import org.dong.Model.FileList;
import org.dong.Tool.Data;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FileDaoImpl extends BeasDao implements FileDao {

    @Override
    public ArrayList<FileList> getList(Map<String,String> map) {

        //创建一个集合
        ArrayList<FileList> list = new ArrayList<FileList>();

//        Connection conn = JdbcUtil.getConnection();
//        PreparedStatement preparedStatement = null;
//        ResultSet rs = null;
        try {
            String sql = "select * from dong_file_list where status = 0 and user_id = ? order by id desc";
            List<Object> fileList = new ArrayList<>();
            fileList.add(map.get("id"));

            List<Object> select = super.select(sql, fileList, FileList.class);
            if(select.size() > 0 ){
                for (Object o : select) {
                    FileList file = (FileList)o;

                    long file_size = Long.parseLong(file.getSize());

                    String bigStr = "";
                    //转换大小
                    int i;
                    for (i = 0; file_size > 1; i++) {


                        if (file_size / 1024 < 1) {
                            break;
                        } else {
                            file_size = file_size / 1024;
                        }

                    }
                    if (i == 0) {
                        bigStr = "B";
                    } else if (i == 1) {
                        bigStr = "KB";
                    } else if (i == 2) {
                        bigStr = "MB";
                    } else if (i == 3) {
                        bigStr = "GB";
                    } else if (i == 4) {
                        bigStr = "TB";
                    }


                    bigStr = file_size + bigStr;
                    file.setSize(bigStr);

                    list.add(file);
                }
            }
            return list;
        } catch (SQLException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public ArrayList<FileList> search(Map<String,String> map) {

        //创建一个集合
        ArrayList<FileList> list = new ArrayList<>();

        try {
            String sql = "select * from dong_file_list where status = 0 and user_id = ? and name = ?";
            List<Object> search = new ArrayList<>();
            search.add(map.get("id"));
            search.add(map.get("str"));
            List<Object> select = super.select(sql, search, FileList.class);
            System.out.println(select);
            if(select.size() > 0 ){
                for (Object o : select) {
                    FileList file = (FileList)o;

                    long file_size = Long.parseLong(file.getSize());

                    String bigStr = "";
                    //转换大小
                    int i;
                    for (i = 0; file_size > 1; i++) {


                        if (file_size / 1024 < 1) {
                            break;
                        } else {
                            file_size = file_size / 1024;
                        }

                    }
                    if (i == 0) {
                        bigStr = "B";
                    } else if (i == 1) {
                        bigStr = "KB";
                    } else if (i == 2) {
                        bigStr = "MB";
                    } else if (i == 3) {
                        bigStr = "GB";
                    } else if (i == 4) {
                        bigStr = "TB";
                    }


                    bigStr = file_size + bigStr;
                    file.setSize(bigStr);

                    list.add(file);
                }
            }
            return list;

        } catch (SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public boolean delFile(Map<String,String> map) {

        try {
            String sql = "UPDATE dong_file_list SET status = 1 WHERE id = ? ";
            List<Object> del = new ArrayList<>();
            del.add(map.get("id"));
            if(super.update(sql,del) >0){
                return true;
            }else{
                return  false;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateName(Map<String,String> map) {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;

        try {
            String sql = "UPDATE dong_file_list SET name = ? WHERE id = ?";
            List<Object> name = new ArrayList<>();
            name.add(map.get("file_name"));
            name.add(map.get("id"));
            if(super.update(sql,name) > 0){
                return true;
            }else{
                return  false;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean upload(Map<String,String> map) {


        try {

            //判断剩余的空间文件是否足够容纳新文件

            String file_name = map.get("file_name");
            BigDecimal file_size = new BigDecimal(map.get("file_size"));
            String src = map.get("file_src");
            int result = Data.user.getLevel_cloud_size().compareTo(Data.user.getCloud_size().add(file_size));

            if(result < 0 ){
                return false;
            }

            long timestamp = System.currentTimeMillis(); // 获取当前时间戳
            BigDecimal bigDecimal = new BigDecimal(timestamp);
            String  sql = "INSERT INTO dong_file_list( `name`, `size`, `src`, `type`,  `create_time`, `update_time`,`user_id`,`status`) VALUES (?,?,?,1,?,?,?,0)";
            List<Object> upload = new ArrayList<>();
            upload.add(file_name);
            upload.add(file_size);
            upload.add(src);
            upload.add(bigDecimal);
            upload.add(bigDecimal);
            upload.add(map.get("id"));

            if(super.update(sql,upload) > 0){

                return true;
            }else{
                return  false;
            }



        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int selFileId(Map<String,String> map) {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        try {
            String sql = "select id from dong_file_list where user_id = ? order BY id desc LIMIT 1";
            List<Object> selFileId = new ArrayList<>();
            selFileId.add(map.get("id"));
            System.out.println(super.selectOne(sql,selFileId,Integer.class));
            return super.selectOne(sql,selFileId,Integer.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
